<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="140px"
      v-loading="formLoading"
    >
      <el-form-item label="设备编号" prop="deviceId">
        <el-input v-model="formData.deviceId" placeholder="请输入设备编号" disabled />
      </el-form-item>
      <el-form-item label="交易日期" prop="transDate">
        <el-date-picker
          v-model="formData.transDate"
          type="date"
          value-format="x"
          placeholder="选择交易日期"
          disabled
        />
      </el-form-item>
      <el-form-item label="批次编码" prop="batchId">
        <el-input v-model="formData.batchId" placeholder="请输入批次编码" disabled />
      </el-form-item>
      <el-form-item label="银行请求流水号" prop="bankSeqId" >
        <el-input v-model="formData.bankSeqId" placeholder="请输入银行请求流水号" disabled />
      </el-form-item>
      <el-form-item label="交易订单金额" prop="transAmt" >
        <el-input v-model="formData.transAmt" placeholder="请输入交易订单金额" disabled />
      </el-form-item>
      <el-form-item label="交易类型编码" prop="eventDefineNo" >
        <el-input v-model="formData.eventDefineNo" placeholder="请输入交易类型编码" disabled />
      </el-form-item>
      <el-form-item label="用户唯一标识" prop="userOpenid">
        <el-input v-model="formData.userOpenid" placeholder="请输入用户唯一标识" disabled />
      </el-form-item>
      <el-form-item label="账号编号" prop="acctId">
        <el-input v-model="formData.acctId" placeholder="请输入账号编号" disabled/>
      </el-form-item>
      <el-form-item label="支付状态" prop="transTate">
        <el-input v-model="formData.transTate" placeholder="请输入支付状态" disabled/>
      </el-form-item>
      <el-form-item label="汇付请求流水号" prop="huifuReqId" >
        <el-input v-model="formData.huifuReqId" placeholder="请输入汇付请求流水号" disabled/>
      </el-form-item>
      <el-form-item label="汇付商户编号" prop="huifuId">
        <el-input v-model="formData.huifuId" placeholder="请输入汇付商户编号" disabled/>
      </el-form-item>
      <el-form-item label="关联的外部订单号" prop="outTransId" >
        <el-input v-model="formData.outTransId" placeholder="请输入关联的外部订单号" disabled />
      </el-form-item>
      <el-form-item label="商户名称" prop="shopName">
        <el-input v-model="formData.shopName" placeholder="请输入商户名称" disabled/>
      </el-form-item>
      <el-form-item label="收款银行名称" prop="bankMerName">
        <el-input v-model="formData.bankMerName" placeholder="请输入收款银行名称" disabled/>
      </el-form-item>
      <el-form-item label="订单支付成功时间" prop="successTime" >
        <el-date-picker
          v-model="formData.successTime"
          type="date"
          value-format="x"
          placeholder="选择订单支付成功时间"
          disabled
        />
      </el-form-item>
      <el-form-item label="支付订单号" prop="partyOrderId">
        <el-input v-model="formData.partyOrderId" placeholder="请输入支付订单号" disabled/>
      </el-form-item>
      <el-descriptions :column="1" label-class-name="desc-label" direction="vertical" border>
      <el-descriptions-item label="回调内容">
        <el-text style="white-space: pre-wrap; word-break: break-word">
          {{ formData.reponseData }}
        </el-text>
      </el-descriptions-item>
    </el-descriptions>
    </el-form>
    <template #footer>
      <!-- <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> -->
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { UnderlineOrderApi, UnderlineOrderVO } from '@/api/pay/underlineorder'

/** 线下支付订单	 表单 */
defineOptions({ name: 'UnderlineOrderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  deviceId: undefined,
  transDate: undefined,
  batchId: undefined,
  bankSeqId: undefined,
  transAmt: undefined,
  eventDefineNo: undefined,
  userOpenid: undefined,
  acctId: undefined,
  transTate: undefined,
  huifuReqId: undefined,
  huifuId: undefined,
  outTransId: undefined,
  shopName: undefined,
  bankMerName: undefined,
  successTime: undefined,
  partyOrderId: undefined,
  reponseData: undefined,
})
const formRules = reactive({
  deviceId: [{ required: true, message: '设备编号不能为空', trigger: 'blur' }],
  bankSeqId: [{ required: true, message: '银行请求流水号不能为空', trigger: 'blur' }],
  transAmt: [{ required: true, message: '交易订单金额不能为空', trigger: 'blur' }],
  eventDefineNo: [{ required: true, message: '交易类型编码不能为空', trigger: 'blur' }],
  acctId: [{ required: true, message: '账号编号不能为空', trigger: 'blur' }],
  transTate: [{ required: true, message: '支付状态不能为空', trigger: 'blur' }],
  outTransId: [{ required: true, message: '关联的外部订单号不能为空', trigger: 'blur' }],
  shopName: [{ required: true, message: '商户名称不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UnderlineOrderApi.getUnderlineOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UnderlineOrderVO
    if (formType.value === 'create') {
      await UnderlineOrderApi.createUnderlineOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await UnderlineOrderApi.updateUnderlineOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deviceId: undefined,
    transDate: undefined,
    batchId: undefined,
    bankSeqId: undefined,
    transAmt: undefined,
    eventDefineNo: undefined,
    userOpenid: undefined,
    acctId: undefined,
    transTate: undefined,
    huifuReqId: undefined,
    huifuId: undefined,
    outTransId: undefined,
    shopName: undefined,
    bankMerName: undefined,
    successTime: undefined,
    partyOrderId: undefined,
    reponseData: undefined,
  }
  formRef.value?.resetFields()
}
</script>